﻿using DBUtility;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SQLite;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinServer
{
    public partial class Form1 : Form
    {

        #region Fileds

        public static string connString = "Data Source =./Bussiness.db;";

        #endregion
        public Form1()
        {
            InitializeComponent();
            this.Text = $"{System.DateTime.Now.ToString("HH:mm")}";
            CreateJob createJob = new CreateJob();
            createJob.Start();           
            CheckForIllegalCrossThreadCalls = false;
            ConstDefine.tcpServer.ConectedEvent += TcpServer_ConectedEvent;
            ConstDefine.tcpServer.RevDataEvent += TcpServer_RevDataDelegateEvent;
            ConstDefine.tcpServer.CreateServer();
            this.WindowState = FormWindowState.Minimized;
        }

        private void Test()
        {
            ConstDefine.UseRecordTableName = "DataRecord" + DateTime.Now.ToString("MMdd");
            var data = MYSQLHelper.GetData(1);
            var con = System.Configuration.ConfigurationManager.AppSettings["DataBaseEnabled"];
            string sql = "DROP TABLE IF EXISTS `data`;CREATE TABLE `data` (  `id` int(11) NOT NULL AUTO_INCREMENT,  `data` binary(1) DEFAULT NULL,  PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=latin1;";
            int result = MYSQLHelper.Update(sql);
            MYSQLHelper.Update(new byte[] { 0x12 });
        }

        private void TcpServer_RevDataDelegateEvent(int message)
        {
            this.label1.Text = $"服务运行中,接收数据长度 {message} {System.DateTime.Now.ToString("HH:mm:ss")}";
        }

        private void TcpServer_ConectedEvent(string message)
        {
            this.toolStripStatusLabel1.Text += message;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            ConstDefine.tcpServer.StopServer();
        }
    }
}